let layer = layui.layer
let query = {
  pagenum: 1,  //页码值
  pagesize: 4,  //每页显示多少条数
}
function renderPage(count) {
    layui.use('laypage', function () {
      let laypage = layui.laypage;
  
      //执行一个laypage实例
      laypage.render({
        elem: 'clicking', //注意，这里的 test1 是 ID，不用加 # 号 
        count,//数据总数，从服务端得到
        limit: 4,
        groups: 2,
        curr: query.pagenum,
        layout: ['prev', 'page', 'next'],
        theme: "#ff4500",
        jump: function (obj, first) {
          //obj包含了当前分页的所有参数，比如：
          // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
          // console.log(obj.limit); //得到每页显示的条数
  
          // 将页码值修改为点击时的当前页
          query.pagenum = obj.curr
          // 将每页显示数据修改为点击时的选择
          query.pagesize = obj.limit
          //首次不执行
          // console.log(123)
          if (!first) {
            // 点击分页的时候发ajax请求
            getNew()
          }
        }
      })
    });
  }
  shouCang()
  function shouCang(){
    axios.get('/user/userDataList',
{params: {
    page: query.pagenum,
    limit: query.pagesize,
    type:1,
    token: localStorage.getItem('token')
  }}).then(({data:{data:{list}}}) => {
    // console.log(list)
    
    const {data, count, pageSize} = list
    const collects = data.map(({pic,title, lovenum, create_date,author,description,click,id}) => {
        const time = create_date
      const date = new Date(time)
      let newTime = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay()
        // console.log(res)
        return `<article class="excerpt excerpt-one">
        <header>
          <a class="cat label label-important" href="">热门<i class="label-arrow"></i></a>
          <h2>
            <a target="_blank" href="show.html" title="${title}">${title}</a>
          </h2>
        </header>
        <p class="text-muted time">
          <a href="">${author}</a> 发布于 ${newTime}
        </p>
        <p class="focus">
          <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
            <span class="item"><span class="thumb-span"><img src="http://124.223.14.236:8060/${pic}"
                  class="thumb" /></span></span></a>
        </p>
        <p class="note">${description}
        </p>
        <p class="text-muted views">
          <span class="post-views">阅读(${click})</span>
          <span class="post-comments">评论(0)</span>
          <a href="javascript:;" class="post-like">
            <i class="fa fa-thumbs-up"></i>赞 (<span>${lovenum}</span>)
          </a>
          <span class="post-tags">
             <button class="btn btn-xs btn-danger collect" data-id="${id}"><i class="fa fa-trash"></i>&nbsp;取消收藏</button>
        </p>
      </article>`
    }).join('')
    $('.list-box').html(collects)
    if(count !== 0){
      $("#count").text(`共${count}条数据, ${Math.ceil(count / pageSize)}页`)
      $('#clicking').show()
    } else {
      $("#count").text(`您还没有收藏哦`)
      $('#clicking').hide()
    } 
    renderPage(count)
  })
  }
// 取消收藏
$('.list-box').on('click', '.collect', function (e){
  e.preventDefault()
  const id = $(this).data('id')
  axios.get('/user/userDataHandle', {params:{
    type: 1,
    article_id: id,
    action: 'del',
    token: localStorage.getItem('token')
  }}).then(() => {
    shouCang()
  })
})